"""
自定义的分页组件，以后如果要想使用这个分页组件，你需要做如下几件事

在视图函数中：
def perrtynum_list(request):

    #1.根据自己的情况筛选自己需要的数据
    data_list = models.PerrtyNum.objects.all()

    #2.实例化分页对象
    page_object = Pagination(request,data_list)

    context = {"data_list": page_object.page_data_list, #分完页的数据
               "value": value,
               "page_string": page_object.html()   #页码
    }


在html中：

{% for obj in data_list %}
{{obj.xx}}
{% endfor %}

<ul class="pagination" style="float:left;">
                {{ page_string }}
            </ul>

"""

from django.utils.safestring import mark_safe

class Pagination(object):

    def __init__(self, request, data_list,page_size=10,page_param="page",plus=5):

        from django.http.request import  QueryDict
        import copy
        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.query_dict = query_dict

        self.page_param = page_param
        page = request.GET.get(page_param, "1")

        if page.isdecimal():
            page = int(page)
        else:
            page = 1

        self.page = page
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size
        self.page_data_list= data_list[self.start:self.end]

        total_count = data_list.count()
        total_page_count, div = divmod(total_count, page_size)
        if div:
            total_page_count += 1
        self.total_page_count = total_page_count
        self.plus = plus


    def html(self):

        if self.total_page_count <= 2 * self.plus + 1:
            start_page = 1
            end_page = self.total_page_count
        else:
            # 当前所在页面小于5
            if self.page <= self.plus:
                start_page = 1
                end_page = 2 * self.plus + 1
            else:
                # 当前页面大于5
                # 当前页面+5 > 总页面数
                if (self.page + self.plus) > self.total_page_count:
                    start_page = self.total_page_count - 2 * self.plus
                    end_page = self.total_page_count
                else:
                    start_page = self.page - self.plus
                    end_page = self.page + self.plus + 1

        page_str_list = []
        self.query_dict.setlist(self.page_param,[1])
        print(self.query_dict.urlencode())


        # 首页
        page_str_list.append('<li><a href="?{}">首页</a></li>'.format(self.query_dict.urlencode()))

        # 上一页
        if self.page > 1:
            prev = '<li><a href="?page={}">上一页</a></li>'.format(self.page - 1)
        else:
            prev = '<li><a href="?page={}">上一页</a></li>'.format(1)
        page_str_list.append(prev)

        # 页面
        for i in range(start_page, end_page + 1):
            if i == self.page:
                ele = '<li class="active"><a href="?page={}">{}</a></li>'.format(i, i)
            else:
                ele = '<li><a href="?page={}">{}</a></li>'.format(i, i)
            page_str_list.append(ele)

        # 下一页
        if self.page < self.total_page_count:
            prev = '<li><a href="?page={}">下一页</a></li>'.format(self.page + 1)
        else:
            prev = '<li><a href="?page={}">下一页</a></li>'.format(self.total_page_count)
        page_str_list.append(prev)

        # 尾页
        page_str_list.append('<li><a href="?page={}">尾页</a></li>'.format(self.total_page_count))

        search_string = """
             <li>
                <form style="float:left;margin-left:-1px" method="get">
                        <input name="page" 
                        style="position:relative;float:left;display:inline-block;width:80px;border-radius:0;"
                        type = "text" class="form-control" placeholder="页码">
                        <button sytle="border-radius:0" class="btn-default" type="submit">跳转</button>
                </form>
            </li>
            """
        page_str_list.append(search_string)

        page_string = mark_safe("".join(page_str_list))

        return page_string